package tz.preprocessing.lexical;


import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



import tz.data.Article;
import tz.data.ReutersArticle;

public class ReutersXMLExtract extends DefaultHandler
{

	boolean reuters = false;
	boolean text = false;
	boolean topics = false;
	String topicsS;
	StringBuffer topicsBuffer;
	StringBuffer textBuffer;
	List<Article> lstArticles;
	ReutersArticle article = null;
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException 
	{
		if(text)
		{
			textBuffer.append(ch, start, length);
			textBuffer.append(' ');
		}
		if(topics){
			topicsBuffer.append(ch, start, length);
			topicsBuffer.append(' ');

		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException 
	{
	
		if(qName.toUpperCase().compareTo("REUTERS") == 0)
		{
			lstArticles.add(article);
			reuters = false;
		}
		if(reuters)
		{
			if(qName.compareTo("TEXT") == 0)
			{
				text = false;
				article.articleText = textBuffer.toString();
			}
			if(qName.compareTo("TOPICS") == 0)
			{
				
				topicsS = topicsBuffer.toString();
				if(topicsS.length()>0){
					//System.out.println("ID "+article.getArticleID()+" topics "+topicsS);
					String [] splited = topicsS.split(" ");
					if(splited.length == 1){
						article.StringTOPICS = topicsS;
						article.hasTOPICS = true;
					}
				}
				topics = false;
			}
		}
	}

	@Override
	public void startDocument() throws SAXException 
	{
		lstArticles = new LinkedList<Article>();
		super.startDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException 
	{
		if(!reuters && qName.toUpperCase().compareTo("REUTERS") == 0)
		{
			article = new ReutersArticle();  
			article.TOPICS = attributes.getValue("TOPICS");
			article.LEWISSPLIT = attributes.getValue("LEWISSPLIT");
			article.CGISPLIT = attributes.getValue("CGISPLIT");
			article.OLDID = Integer.parseInt(attributes.getValue("OLDID"));
			article.NEWID = Integer.parseInt(attributes.getValue("NEWID"));
			reuters = true;
		}
		if(reuters)
		{
			if(qName.compareTo("TEXT") == 0)
			{
				text = true;
				textBuffer = new StringBuffer();
			}
			if(qName.compareTo("TOPICS") == 0){
				topics = true;
				topicsBuffer = new StringBuffer();

			}
		}
		
	}

	public static List<Article> readAllArticles(File f) throws ParserConfigurationException, SAXException, IOException
	{
		SAXParserFactory factory = SAXParserFactory.newInstance();
		factory.setValidating(false);
		SAXParser parser = factory.newSAXParser();
		ReutersXMLExtract reutersArticles  = new ReutersXMLExtract();
		parser.parse(f, reutersArticles);
		return reutersArticles.lstArticles;
	}
	
}

